﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Identity;

namespace AbpProjectTemplate.Identity
{
    /// <summary>
    /// 用户
    /// </summary>
    [Authorize]
    //[ServieGroup(ServiceGroup.Manager)]
    public class UserAppService : IdentityUserAppService
    {
        public UserAppService(IdentityUserManager userManager,
                              IIdentityUserRepository userRepository,
                              IIdentityRoleRepository roleRepository,
                              IOptions<IdentityOptions> identityOptions,
                              IPermissionChecker permissionChecker) 
            : base(userManager, userRepository, roleRepository, identityOptions, permissionChecker)
        {
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        [Authorize(IdentityPermissions.Users.Update)]
        public async Task UpdatePasswordAsync(Guid id, IdentityUserPasswordUpdateInput input)
        {
            await IdentityOptions.SetAsync();

            var user = await UserManager
                .FindByIdAsync(id.ToString());

            var r = await UserManager
                 .RemovePasswordAsync(user);
            r.CheckErrors();
            await UserManager.AddPasswordAsync(user, input.NewPassword);

            r.CheckErrors();
        }

        ///// <summary>
        ///// 创建管理员（默认添加manager角色）
        ///// </summary>
        //public Task<IdentityUserDto> CreateManagerAsync(IdentityUserCreateDto input)
        //{
        //	input.RoleNames = input.RoleNames.IsNullOrEmpty()
        //		? new string[0]
        //		: input.RoleNames;
        //	if (!input.RoleNames.Contains(AppRoleConsts.ManagerRole))
        //		input.RoleNames.Append(AppRoleConsts.ManagerRole);
        //	return base.CreateAsync(input);
        //}

        ///// <summary>
        ///// 获取管理员列表
        ///// </summary>
        //public async Task<PagedResultDto<IdentityUserDto>> GetMangersAsync(GetIdentityUsersInput input)
        //{
        //	var managerRole = await this.RoleRepository
        //		.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(AppRoleConsts.ManagerRole));
        //	var count = await UserRepository.GetCountAsync(input.Filter, roleId: managerRole.Id);
        //	var list = await UserRepository.GetListAsync(input.Sorting, input.MaxResultCount, input.SkipCount, input.Filter, roleId: managerRole.Id);

        //	return new PagedResultDto<IdentityUserDto>(
        //		count,
        //		ObjectMapper.Map<List<IdentityUser>, List<IdentityUserDto>>(list)
        //	);
        //}
    }
}
